<template>
  <div class="tags">
    <el-button round v-for="tag in types" @click="changeType(tag.id)" :class="{'active':type===tag.id}">
      {{ tag.name }}
    </el-button>
  </div>
  <div class="tags">
    <el-button round v-for="tag in areas" @click="changeArea(tag.id)" :class="{'active':area===tag.id}">
      {{ tag.name }}
    </el-button>
  </div>
  <div class="textZimu">
    <span round @click="changeZimu('-1')" :class="{'active-text':'-1'===zimu}">
      热门
    </span>
    <el-divider direction="vertical" />
    <span v-for="tag in alphabet" @click="changeZimu(tag)" :class="['zmspan', { 'active-text': tag === zimu }]">
      {{ tag }}
    </span>
    <span @click="changeZimu('#')" :class="['zmspan', { 'active-text': '#' === zimu }]">#</span>
  </div>
  <div class="body">
    <div class="card" v-for="item in singerList" @click="toSingerListPage(item)">
      <div class="image" :style="`background: url(${item.picUrl}) center / cover;`">
        <el-icon>
          <i-bi-pause-fill v-if="item.id === playSingerId && isPlayed" @click.stop="stopPlay"/>
          <i-bi-play-fill v-else @click.stop="playAll(item.id)" class="playicon"/>
        </el-icon>
      </div>
      <span>{{item.name}}</span>
      <span class="count">单曲：{{item.musicSize}}</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, reactive, ref } from "vue";
import {artistList,artistsSongs} from "@/assets/apis/http";
import {Singer,Song} from "@/assets/apis/MusicDataType";
import util from "@/tools/util";
import {usePlayStore} from "@/store/PlayStore";
import emitter from "@/store/emitter";
import { useRouter } from "vue-router";

const router = useRouter()
const playstore = usePlayStore()
const isPlayed = computed(()=>playstore.isPlayed)
const playSingerId = computed(()=>playstore.playListId)
const types = [
  {id:-1,name:"全部"},
  {id:1,name:"男歌手"},
  {id:2,name:"女歌手"},
  {id:3,name:"乐队"}
]
const areas = [
  {id:-1,name:"全部"},
  {id:7,name:"华语"},
  {id:96,name:"欧美"},
  {id:8,name:"日本"},
  {id:16,name:"韩国"},
  {id:0,name:"其他"}
]
const alphabet = Array.from({ length: 26 }, (_, i) => String.fromCharCode(i + 65));
const type =ref<number>(-1)
const area =ref<number>(-1)
const zimu =ref<string>('-1')
const singerList =reactive<Singer[]>([])
async function getHotSinger() {
  let res = await artistList({ type: type.value, area: area.value, initial: zimu.value, limit: 50, offset: 0 })
  let list = res.artists.map(songItem => {
    return {
      id: songItem.id,
      name: songItem.name,
      picUrl: util.formatSongsPic(songItem.picUrl,200),
      musicSize: songItem.musicSize,
      albumSize: songItem.albumSize,
      followed: songItem.followed,
      alias: songItem.alias[0]
    }
  })
  console.log("查询歌手", list)
  singerList.splice(0, singerList.length, ...list)
}

function toSingerListPage(item:Singer){
  router.push({path:'/singers/'+item.id+'/song',query: {name:item.name,picUrl:item.picUrl,alias:item.alias,musicSize:item.musicSize,albumSize:item.albumSize}})
}
async function playAll (id:number){
  //如果暂停之前正处于播放当前歌单，仅仅是切换播放状态
  if (playstore.playListId === id) {
    emitter.emit('playAudioType','play')
    return
  }
  let res = await artistsSongs({ id: id });
  // console.log("查询榜单内歌曲",res)
  let newSongs: Song[] = res.songs.map(song => {
    return {
      id: song.id || null,
      name: song.name || '未知',
      mvId: song.mv,
      albumPic: util.formatSongsPic(song.al.picUrl,200),
      singer: song.ar.map(({ name, id }) => ({ name, id })),
      album: song.al.name,
      alia: song.alia[0],
      duration: song.dt,
      url: "https://music.163.com/song/media/outer/url?id="+song.id+".mp3",
      fee: song.fee,
      publishTime: song.publishTime
    };
  });
  playstore.playAll(newSongs)
  playstore.playListId = id
}
function stopPlay(){
  emitter.emit('playAudioType','play')
}
function changeType(t:number){
  type.value=t
  getHotSinger()
}
function changeArea(t:number){
  area.value=t
  getHotSinger()
}
function changeZimu(t:string){
  zimu.value=t
  getHotSinger()
}
getHotSinger()
</script>
<style scoped lang="scss">
.tags{
  display: flex;
  margin: 0 20px 20px 20px;
  .active{
    background-color: var(--my-bg-menu-active);
  }
  .active-text{
    color: var(--my-bg-menu-active);
  }
}
.textZimu{
  display: flex;
  margin: 0 20px 20px 20px;
  span{
    cursor: pointer;
  }
  .zmspan {
    cursor: pointer;
    display: inline-block;
    flex-basis: 25px;
  }
  .active{
    color: var(--my-bg-menu-active);
  }
  .active-text{
    color: var(--my-bg-menu-active);
  }
}
.body {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
  margin-left: 10px;
  margin-right: 10px;
  .card{
    display: flex;
    flex-direction: column;
    width: 180px;
    height: 210px;
    align-items: center;
    justify-content: center;
    border-radius: 20px;
    cursor: pointer;
    &:hover{
      background-color: var(--transparent-2);
      .image i .playicon{
        visibility: inherit;
      }
    }
    .image {
      display: grid;
      place-items: center;
      margin-bottom: 10px;
      width: 150px;
      height: 150px;
      border-radius: 75px;
      object-fit: cover;
      i {
        font-size: 50px;
        .playicon{
          visibility: hidden;
        }
      }
    }
    .count{
      color: var(--transparent-3);
      font-size: 14px;
      padding-top: 5px;
    }
  }
}

</style>
